package com.longrise.co.ui.mine.presenter;

import android.app.Activity;
import android.app.Dialog;
import android.os.Handler;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.co.api.ApiConstants;
import com.longrise.co.api.CommonPara;
import com.longrise.co.ui.carfiles.model.CarFilesModel;
import com.longrise.co.ui.carfiles.model.UploadBean;
import com.longrise.co.ui.home.model.BindCarInfo;
import com.longrise.co.ui.home.model.HomeModel;
import com.longrise.co.ui.login.model.LoginModel;
import com.longrise.co.ui.login.model.SmsCodeBean;
import com.longrise.co.ui.mine.contract.MineContract;
import com.longrise.co.ui.mine.model.ComplaintDetailBean;
import com.longrise.co.ui.mine.model.EditCarBean;
import com.longrise.co.ui.mine.model.EvaluationDetailBean;
import com.longrise.co.ui.mine.model.Message;
import com.longrise.co.ui.mine.model.MessageDetail;
import com.longrise.co.ui.mine.model.MineComplaintBean;
import com.longrise.co.ui.mine.model.MineEvaluationBean;
import com.longrise.co.ui.mine.model.UserInfo;
import com.longrise.co.ui.mine.model.VerifiedCarBean;
import com.ly.common.baserx.RxSubscriber;
import com.ly.common.commonutils.StringUtils;
import com.ly.common.commonwidget.LoadingDialog;
import com.ly.common.security.EnCryptDeCrypt1;
import com.ly.common.security.EnCryptDeCrypt2;

/**
 * Created by luoyang on 2017-6-23.
 */

public class MinePresenter extends MineContract.Presenter {

    private HomeModel homeModel = new HomeModel();
    private CarFilesModel carFilesModel = new CarFilesModel();
    private LoginModel loginModel = new LoginModel();

    @Override
    public void getUserInfo(boolean isProcess) {
        try {

            EntityBean bean = new EntityBean();

            bean.set("userflag", CommonPara.getInstance().getUserFlag());
            bean.set("user", CommonPara.getInstance().getUser());
            bean.set("sign", CommonPara.getInstance().getEnUserFlag());

            mRxManage.add(mModel.userInfoRequest(bean).subscribe(new RxSubscriber<UserInfo>(mContext, isProcess) {
                @Override
                protected void _onNext(UserInfo bean) {
                    mView.requestSuccess(bean);
                }

                @Override
                protected void _onError(String message) {
                    mView.showErrorTip(message);
                }

                @Override
                public void onCompleted() {
                    super.onCompleted();
                    mView.onRequestComplete();
                }
            }));

        } catch (Exception e) {
            e.printStackTrace();
            mView.showErrorTip("请求失败");
        }
    }

    @Override
    public void modifyUserInfo(EntityBean bean) {
        try {
            mRxManage.add(mModel.modifyUserInfoRequest(bean).subscribe(new RxSubscriber<String>(mContext, true) {
                @Override
                protected void _onNext(String bean) {
                    if(mView instanceof MineContract.MineView) {
                        ((MineContract.MineView) mView).modifySuccess();
                    } else {
                        mView.requestSuccess(bean);
                    }
                }

                @Override
                protected void _onError(String message) {
                    mView.showErrorTip(message);
                }


                @Override
                public void onCompleted() {
                    super.onCompleted();
                    mView.onRequestComplete();
                }
            }));

        } catch (Exception e) {
            e.printStackTrace();
            mView.showErrorTip("请求失败");
        }
    }

    @Override
    public void getMyCarList(String carNo, boolean isShowProcess) {
        try {
            EntityBean bean = new EntityBean();

            if(!StringUtils.isEmpty(carNo)) {
                bean.set("carno", carNo);
            }
            bean.set("userflag", CommonPara.getInstance().getUserFlag());
            bean.set("sign", CommonPara.getInstance().getEnUserFlag());
            bean.set("user", CommonPara.getInstance().getUser());

            mRxManage.add(homeModel.bindCarListRequest(bean).subscribe(new RxSubscriber<BindCarInfo>(mContext, isShowProcess) {
                @Override
                protected void _onNext(BindCarInfo bean) {
                    mView.requestSuccess(bean);
                }

                @Override
                protected void _onError(String message) {
                    mView.showErrorTip(message);
                }

                @Override
                public void onCompleted() {
                    super.onCompleted();
                    mView.onRequestComplete();
                }
            }));

        } catch (Exception e) {
            e.printStackTrace();
            mView.showErrorTip("请求失败");
        }
    }

    @Override
    public void userFeedBack(String content) {
        final Dialog mLoadingDialog = LoadingDialog.showDialogForLoading((Activity) mContext,"反馈中...",true);
        mLoadingDialog.show();
        new Handler().postDelayed(new Runnable() {
            public void run() {
                mLoadingDialog.dismiss();
                mView.requestSuccess(null);
            }

        }, 2000);
    }

    @Override
    public void getComplaintList(String carNo, int pageNo, boolean isProcess) {
        try {
            EntityBean bean = new EntityBean();

            if(!StringUtils.isEmpty(carNo)) {
                bean.set("carno", carNo);
            }
            bean.set("userflag", CommonPara.getInstance().getUserFlag());
            bean.set("sign", CommonPara.getInstance().getEnUserFlag());
            bean.set("user", CommonPara.getInstance().getUser());
            bean.set("pagenum", pageNo);
            bean.set("pagesize", ApiConstants.PAGE_SIZE_10);

            mRxManage.add(mModel.complaintListRequest(bean).subscribe(new RxSubscriber<MineComplaintBean>(mContext, isProcess) {
                @Override
                protected void _onNext(MineComplaintBean bean) {
                    mView.requestSuccess(bean);
                }

                @Override
                protected void _onError(String message) {
                    mView.showErrorTip(message);
                }

                @Override
                public void onCompleted() {
                    super.onCompleted();
                    mView.onRequestComplete();
                }
            }));

        } catch (Exception e) {
            e.printStackTrace();
            mView.showErrorTip("请求失败");
        }

    }

    @Override
    public void getEvaluationList(String carNo, int pageNo, boolean isProcess) {
        try {
            EntityBean bean = new EntityBean();

            if(!StringUtils.isEmpty(carNo)) {
                bean.set("carno", carNo);
            }
            bean.set("userflag", CommonPara.getInstance().getUserFlag());
            bean.set("sign", CommonPara.getInstance().getEnUserFlag());
            bean.set("user", CommonPara.getInstance().getUser());
            bean.set("pagenum", pageNo);
            bean.set("pagesize", ApiConstants.PAGE_SIZE_10);

            mRxManage.add(mModel.evaluationListRequest(bean).subscribe(new RxSubscriber<MineEvaluationBean>(mContext, isProcess) {
                @Override
                protected void _onNext(MineEvaluationBean bean) {
                    mView.requestSuccess(bean);
                }

                @Override
                protected void _onError(String message) {
                    mView.showErrorTip(message);
                }

                @Override
                public void onCompleted() {
                    super.onCompleted();
                    mView.onRequestComplete();
                }
            }));

        } catch (Exception e) {
            e.printStackTrace();
            mView.showErrorTip("请求失败");
        }
    }

    @Override
    public void getComplaintDetail(String complaintId, String areaId) {
        try {
            if(StringUtils.isEmpty(complaintId) || StringUtils.isEmpty(areaId)) {
                return ;
            }
            EntityBean bean = new EntityBean();

            String enComplaintId = EnCryptDeCrypt2.getInstance().EnCrypt(complaintId);

            bean.set("complaintid", complaintId);
            bean.set("areaid", areaId);
            bean.set("sign", enComplaintId);
            bean.set("user", CommonPara.getInstance().getUser());

            mRxManage.add(mModel.complaintDetailRequest(bean).subscribe(new RxSubscriber<ComplaintDetailBean>(mContext, true) {
                @Override
                protected void _onNext(ComplaintDetailBean bean) {
                    mView.requestSuccess(bean);
                }

                @Override
                protected void _onError(String message) {
                    mView.showErrorTip(message);
                }

                @Override
                public void onCompleted() {
                    super.onCompleted();
                    mView.onRequestComplete();
                }
            }));

        } catch (Exception e) {
            e.printStackTrace();
            mView.showErrorTip("请求失败");
        }
    }

    @Override
    public void verifiedCar(EntityBean bean) {
        try{
            mRxManage.add(mModel.verifiedCarRequest(bean).subscribe(new RxSubscriber<VerifiedCarBean>(mContext, true) {
                @Override
                protected void _onNext(VerifiedCarBean bean) {
                    mView.requestSuccess(bean);
                }

                @Override
                protected void _onError(String message) {
                    mView.showErrorTip(message);
                }

                @Override
                public void onCompleted() {
                    super.onCompleted();
                    mView.onRequestComplete();
                }
            }));

        } catch (Exception e) {
            e.printStackTrace();
            mView.showErrorTip("请求失败");
        }
    }

    @Override
    public void getEvaluationDetail(String evaluationId, String areaId) {
        try {
            if(StringUtils.isEmpty(evaluationId) || StringUtils.isEmpty(areaId)) {
                return ;
            }
            EntityBean bean = new EntityBean();

            String enComplaintId = EnCryptDeCrypt2.getInstance().EnCrypt(evaluationId);

            bean.set("evaluateid", evaluationId);
            bean.set("areaid", areaId);
            bean.set("sign", enComplaintId);
            bean.set("user", CommonPara.getInstance().getUser());

            mRxManage.add(mModel.evaluationDetailRequest(bean).subscribe(new RxSubscriber<EvaluationDetailBean>(mContext, true) {
                @Override
                protected void _onNext(EvaluationDetailBean bean) {
                    mView.requestSuccess(bean);
                }

                @Override
                protected void _onError(String message) {
                    mView.showErrorTip(message);
                }

                @Override
                public void onCompleted() {
                    super.onCompleted();
                    mView.onRequestComplete();
                }
            }));

        } catch (Exception e) {
            e.printStackTrace();
            mView.showErrorTip("请求失败");
        }
    }

    @Override
    public void uploadPhoto(String imageInfo, String type) {
        try {
            if(StringUtils.isEmpty(imageInfo) || StringUtils.isEmpty(type)) {
                mView.showErrorTip("上传照片失败，参数有误");
                return ;
            }
            EntityBean bean = new EntityBean();

            bean.set("imageInfo", imageInfo);
            bean.set("type", type);

            mRxManage.add(carFilesModel.uploadPhotoRequest(bean).subscribe(new RxSubscriber<UploadBean>(mContext, true) {
                @Override
                protected void _onNext(UploadBean bean) {
                    if(mView instanceof MineContract.AuthCarView) {
                        ((MineContract.AuthCarView) mView).uploadSuccess(bean);
                    }
                }

                @Override
                protected void _onError(String message) {
                    mView.showErrorTip(message);
                }
            }));

        } catch (Exception e) {
            e.printStackTrace();
            mView.showErrorTip("上传图片失败");
        }
    }

    @Override
    public void sendSmsRequest(String mobilePhone, String type) {
        try {
            if (StringUtils.isEmpty(mobilePhone) || StringUtils.isEmpty(type)) {
                mView.showErrorTip("请输入手机号");
                return;
            }
            if(!com.ly.common.commonutils.StringUtils.isChinaPhoneLegal(mobilePhone)) {
                mView.showErrorTip("请输入正确的手机号");
                return ;
            }

            EntityBean bean = new EntityBean();
            bean.set("mobile", mobilePhone);
            bean.set("type", type);

            mRxManage.add(loginModel.sendSms(bean).subscribe(new RxSubscriber<SmsCodeBean>(mContext, true) {
                @Override
                protected void _onNext(SmsCodeBean detail) {
                    if(mView instanceof MineContract.AuthCarView) {
                        ((MineContract.AuthCarView) mView).sendMsgSuccess();
                    }
                }

                @Override
                protected void _onError(String message) {
                    mView.showErrorTip(message);
                }
            }));

        } catch (Exception e) {
            e.printStackTrace();
            mView.showErrorTip("请求失败");
        }
    }

    @Override
    public void modifyPwd(String mobilePhone, String oldPwd, String newPwd, String confirmNewPwd) {
        try {
            if(StringUtils.isEmpty(mobilePhone)) {
                mView.showErrorTip("请输入手机号");
                return;
            }
            if(StringUtils.isEmpty(oldPwd)) {
                mView.showErrorTip("请输入旧密码");
                return;
            }
            if(StringUtils.isEmpty(newPwd)) {
                mView.showErrorTip("请输入新密码");
                return;
            }
            if(StringUtils.isEmpty(confirmNewPwd)) {
                mView.showErrorTip("请输入确认新密码");
                return;
            }

            if(!StringUtils.isChinaPhoneLegal(mobilePhone)) {
                mView.showErrorTip("请输入正确的手机号");
                return;
            }

            if(oldPwd.equals(newPwd)) {
                mView.showErrorTip("修改密码不能与原密码一致");
                return;
            }

            if(!confirmNewPwd.equals(newPwd)) {
                mView.showErrorTip("确认密码与输入密码不一致");
                return;
            }

            EntityBean bean = new EntityBean();
            String enMobile = EnCryptDeCrypt2.getInstance().EnCrypt(mobilePhone);
            String enOldPwd = EnCryptDeCrypt1.getInstance().EnCrypt(oldPwd);
            String enNewPwd = EnCryptDeCrypt1.getInstance().EnCrypt(newPwd);

            bean.set("mobile", mobilePhone);
            bean.set("user", CommonPara.getInstance().getUser());
            bean.set("oldpassword", enOldPwd);
            bean.set("newpassword", enNewPwd);
            bean.set("sign", enMobile);

            mRxManage.add(mModel.modifyPwdRequest(bean).subscribe(new RxSubscriber<String>(mContext, true) {
                @Override
                protected void _onNext(String s) {
                   mView.requestSuccess(s);
                }

                @Override
                protected void _onError(String message) {
                    mView.showErrorTip(message);
                }

                @Override
                public void onCompleted() {
                    super.onCompleted();
                    mView.onRequestComplete();
                }
            }));

        } catch (Exception e) {
            e.printStackTrace();
            mView.showErrorTip("请求失败");
        }

    }

    @Override
    public void deleteCar(String carId) {
        try {
            if (StringUtils.isEmpty(carId)) {
                mView.showErrorTip("车辆id为空");
                return;
            }

            String enCarId = EnCryptDeCrypt2.getInstance().EnCrypt(carId);
            EntityBean bean = new EntityBean();
            bean.set("carid", carId);
            bean.set("userflag", CommonPara.getInstance().getUserFlag());
            bean.set("sign", enCarId);
            bean.set("user", CommonPara.getInstance().getUser());

            mRxManage.add(mModel.deleteCarRequest(bean).subscribe(new RxSubscriber<String>(mContext, true) {
                @Override
                protected void _onNext(String detail) {
                   if(mView instanceof MineContract.MyCarView) {
                        ((MineContract.MyCarView) mView).deleteCarSuccess();
                   }
                }

                @Override
                protected void _onError(String message) {
                    mView.showErrorTip(message);
                }

                @Override
                public void onCompleted() {
                    super.onCompleted();
                    mView.onRequestComplete();
                }
            }));

        } catch (Exception e) {
            e.printStackTrace();
            mView.showErrorTip("请求失败");
        }
    }

    @Override
    public void editCar(EntityBean bean) {
        try {

            mRxManage.add(mModel.editCarRequest(bean).subscribe(new RxSubscriber<EditCarBean>(mContext, true) {
                @Override
                protected void _onNext(EditCarBean bean) {
                    if(mView instanceof MineContract.MyCarView) {
                        ((MineContract.MyCarView) mView).modifyCarSuccess();
                    } else {
                        mView.requestSuccess(bean);
                    }
                }

                @Override
                protected void _onError(String message) {
                    mView.showErrorTip(message);
                }

                @Override
                public void onCompleted() {
                    super.onCompleted();
                    mView.onRequestComplete();
                }
            }));

        } catch (Exception e) {
            e.printStackTrace();
            mView.showErrorTip("请求失败");
        }
    }

    @Override
    public void getMessageList() {
        try {
            EntityBean bean = new EntityBean();
            CommonPara commonPara = CommonPara.getInstance();

            bean.set("userflag", commonPara.getUserFlag());
            bean.set("userid", commonPara.getmUserId());
            bean.set("user", commonPara.getUser());
            bean.set("sign", commonPara.getEnUserFlag());

            mRxManage.add(mModel.messageList(bean).subscribe(new RxSubscriber<Message>(mContext, true) {
                @Override
                protected void _onNext(Message bean) {
                    mView.requestSuccess(bean);
                }

                @Override
                protected void _onError(String message) {
                    mView.showErrorTip(message);
                }

                @Override
                public void onCompleted() {
                    super.onCompleted();
                    mView.onRequestComplete();
                }
            }));

        } catch (Exception e) {
            e.printStackTrace();
            mView.showErrorTip("请求失败");
        }
    }

    @Override
    public void getMessageDetail(String mid, String companyId) {
        try {
            if(StringUtils.isEmpty(mid) || StringUtils.isEmpty(companyId)) {
                mView.showErrorTip("请求失败");
                return ;
            }
            EntityBean bean = new EntityBean();
            CommonPara commonPara = CommonPara.getInstance();
            String enMid = EnCryptDeCrypt2.getInstance().EnCrypt(mid);

            bean.set("mid", mid);
            bean.set("companyid", companyId);
            bean.set("areaid", commonPara.getAreaId());
            bean.set("user", commonPara.getUser());
            bean.set("sign", enMid);

            mRxManage.add(mModel.messageDetail(bean).subscribe(new RxSubscriber<MessageDetail>(mContext, true) {
                @Override
                protected void _onNext(MessageDetail bean) {
                    mView.requestSuccess(bean);
                }

                @Override
                protected void _onError(String message) {
                    mView.showErrorTip(message);
                }

                @Override
                public void onCompleted() {
                    super.onCompleted();
                    mView.onRequestComplete();
                }
            }));

        } catch (Exception e) {
            e.printStackTrace();
            mView.showErrorTip("请求失败");
        }
    }

}
